// order/mycar/mycar.js
const app = getApp();

Page({

    data: {
        myCarList: [],
        isChoose:false,
        isBack:true
    },
    onUnload(){
        // console.log(this.data.isBack)
        // if (this.data.isBack){
        //     this.navigateToPage();
        // }
        return;
    },
    navigateToPage(){
        if (this.data.isChoose) {
            wx.redirectTo({
                url: '/pages/index/index',
            })
        } else {
            wx.navigateTo({
                url: '/pages/Personal/Personal',
            })
        }
    },
    onLoad: function() {
        let isChoose = app.tempData.isChoose
        console.log("isChoose",isChoose);
        if (isChoose){
            this.setData({ isChoose: isChoose});
        }
        if (!app.globalData.hasUser) {
            app.userInfoReadyCallback = this.getMyCarList;
        } else {
            this.getMyCarList();
        }
        //清空要修改的车辆信息缓存
        wx.removeStorageSync('editCarData');
    },
    chooseMyCar(event) {
        this.data.isBack=false;
        //同步
        console.log(event);
        wx.setStorageSync('chooseCar', event.currentTarget.dataset.item);
        wx.redirectTo({
            url: '/pages/index/index',
        })
    },
    getMyCarList: function() {
        // let carList = app.tempData.myCarList;
        // if (carList) {
        //     this.setData({
        //         myCarList: carList
        //     })
        //     return;
        // }
        let url = "/user/userCar/findUserCarList";
        let param = {
            userId: app.globalData.user.id
        }
        let that = this;
        app.getJson(url, param, function(res) {
            if (res.code == 0) {
                console.log('getMyCarList for server')
                that.setData({
                    myCarList: res.data
                })
                // wx.setStorageSync('myCarList',res.data);
                app.tempData.myCarList = res.data;
            } else {
                wx.showToast({
                    title: '获取车辆列表失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    // 编辑
    updateMyCar(e) {
        let carInfo = e.target.dataset.carinfo;
        carInfo.defaultCar = carInfo.defaultCar == 1;
        //保存要编辑的数据
        app.tempData.editCarData = carInfo;
        wx.navigateTo({
            url: '../../pages/addcarpage/addcarpage?method=update' 
        })
    },

    deleteMyCar: function(event) {
        let url = "/user/userCar/deleteUserCar";
        console.log(event)
        let param = {
            id: event.target.dataset.carid
        }
        let that = this;
        app.getJson(url, param, function(res) {
            if (res.code == 0) {
                app.tempData.myCarList.splice(event.target.dataset.idx, 1)
                let myCarList = that.data.myCarList;
                myCarList.splice(event.target.dataset.idx, 1)
                that.setData({
                    myCarList: myCarList
                })
               
                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: '获取车辆列表失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    setMyCarDefault(event) {
        let url = "/user/userCar/updateUserCar";
        console.log(event)
        let param = {
            id: event.target.dataset.carid,
            userId:app.globalData.user.id,
            defaultCar: 1
        }
        let that = this;
        app.getJson(url, param, function(res) {
            if (res.code == 0) {
                for (let i in that.data.myCarList){
                    if(that.data.myCarList[i].defaultCar==1){
                        that.data.myCarList[i].defaultCar =0;
                        app.tempData.myCarList[i].defaultCar =0;
                    }
                    if (event.target.dataset.idx == i){
                        let car = that.data.myCarList[event.target.dataset.idx];
                        car.defaultCar = 1
                        that.data.myCarList[event.target.dataset.idx] = that.data.myCarList[0];
                        that.data.myCarList[0] = car;
                    }
                }
                // that.data.myCarList[event.target.dataset.idx].defaultCar=1;
                that.setData({
                    myCarList: that.data.myCarList
                })
                app.tempData.myCarList[event.target.dataset.idx].defaultCar = 1;
                wx.showToast({
                    title: '设置成功',
                    icon: 'success',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: '设置车辆失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})